package com.doule.farmerassistant.plan;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;

import com.doule.farmerassistant.R;
import com.doule.farmerassistant.DeathConfig;

import android.net.http.AndroidHttpClient;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

/**
 * download apk from server.
 * 
 * @author happy
 * 
 */
public final class DeletePlanTask {

    /**
     * Constructor.
     * 
     * @param context
     *            PlanFragment
     * @param id
     *            Plan id.
     * @param token
     *            Authentication token.
     */
    public DeletePlanTask(PlanFragment context, final String id,
            final String token) {
        mContext = context;
        mId = id;
        mToken = token;
    }

    /**
     * the APK info object of which to be download.
     */
    // private String mToken;

    private PlanFragment mContext;

    /**
     * Server url.
     */
    private String mServerUrl;
    /**
     * Plan id.
     */
    private String mId;
    /**
     * Authentication token.
     */
    private String mToken;

    /**
     * Delete plan.
     * 
     * @return true if delete success.
     */
    private boolean deletePlanTask() {

        mServerUrl = DeathConfig.getServerUrl();
        if (mServerUrl == null) {
            return false;
        }

        Log.e("LoadMessageTask", "server url: " + mServerUrl);
        HttpPost request = new HttpPost(mServerUrl + "/deletePlan");
        request.setHeader("content-type", "application/json");
        request.setHeader("planId", mId);

        request.setHeader("tk", mToken);
        AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        try {
            HttpResponse resp = client.execute(request);
            Header head = resp.getFirstHeader("error");
            if (null != head) {
                String error = head.getValue();
                Log.e("UserLoginTask", "login error: " + error);
                return false;
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
        return false;
    }

    /**
     * worker runnable object.
     */
    private final Runnable worker = new Runnable() {

        @Override
        public void run() {
            boolean task = deletePlanTask();
            if (task) {
                Looper.prepare();
                Toast.makeText(mContext.getActivity(),
                        mContext.getString(R.string.delete_plan_success),
                        Toast.LENGTH_LONG).show();
                mContext.updateList();
                Looper.loop();
            } else {
                Looper.prepare();
                Toast.makeText(mContext.getActivity(),
                        mContext.getString(R.string.delete_plan_fail),
                        Toast.LENGTH_LONG).show();
                mContext.updateList();
                Looper.loop();
            }
        }
    };

    /**
     * Start working thread.
     */
    public void go() {
        new Thread(worker).start();
    }
}
